﻿using System;
using System.Collections.Generic;
using ETModel;

namespace ETHotfix {
    [ObjectSystem]
    public class ConfigComponentAwakeSystem : AwakeSystem<ConfigComponent, string> {
        public override void Awake(ConfigComponent self, string ConfigFileInfo) {
            self.Awake(ConfigFileInfo);
        }
    }

    //不使用load事件触发加载,一个是为了调用者灵活使用,另一个是为了传入指定配置加载文件
    //[ObjectSystem]
    //public class ConfigComponentLoadSystem : LoadSystem<ConfigComponent> {
    //    public override void Load(ConfigComponent self) {
    //        self.Load();
    //    }
    //}

    /// <summary>
    /// 客户端Hotfix层配置组件拓展
    /// </summary>
    public static class ConfigComponentHelper {
        public static void Awake(this ConfigComponent self, string ConfigFileInfo) {
            self.AllConfig.Clear();
            List<Type> types = Game.EventSystem.GetTypes();

            foreach (Type type in types) {
                object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
                if (attrs.Length == 0) {
                    continue;
                }

                ConfigAttribute configAttribute = attrs[0] as ConfigAttribute;
                // 只加载指定的配置
                if (!configAttribute.Type.Is(AppType.ClientH)) {
                    continue;
                }

                object obj = Activator.CreateInstance(type);

                ACategory iCategory = obj as ACategory;
                if (iCategory == null) {
                    throw new Exception($"class: {type.Name} not inherit from ACategory");
                }
                iCategory.ConfigFileInfo = ConfigFileInfo;
                iCategory.BeginInit();
                iCategory.EndInit();

                self.AllConfig[iCategory.ConfigType] = iCategory;
            }
        }
    }
}